package com.shxzz.easy12306.services.user.controller;

import com.shxzz.easy12306.framework.starter.convention.result.Result;
import com.shxzz.easy12306.framework.starter.convention.result.Results;
import com.shxzz.easy12306.services.user.dto.req.UserLoginReqDTO;
import com.shxzz.easy12306.services.user.dto.req.UserRegisterReqDTO;
import com.shxzz.easy12306.services.user.dto.resp.UserLoginRespDTO;
import com.shxzz.easy12306.services.user.dto.resp.UserRegisterRespDTO;
import com.shxzz.easy12306.services.user.service.UserLoginService;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户登录控制层
 * @author shxzz
 * @create 2024-02-27-20:55
 */
@RequiredArgsConstructor
@RestController
public class UserLoginController {

    private final UserLoginService userLoginService;

    /**
     * 用户登录
     */
    @PostMapping("/api/user-service/v1/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userLoginService.login(requestParam));
    }

    /**
     * 通过 JWT Token 检查用户是否登录
     */
    @GetMapping("/api/user-service/check-login")
    public Result<UserLoginRespDTO> checkLogin(@RequestParam("accessToken") String accessToken) {
        return Results.success(userLoginService.checkLogin(accessToken));
    }

    /**
     * 查询用户名是否可用
     */
    @GetMapping("/api/user-service/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") @NotEmpty String username) {
        return Results.success(userLoginService.isUsernameAvailable(username));
    }

    /**
     * 用户注册
     */
    @PostMapping("/api/user-service/register")
    public Result<UserRegisterRespDTO> register(@RequestBody UserRegisterReqDTO requestParam) {
        return Results.success(userLoginService.register(requestParam));
    }

    /**
     * 用户退出登录
     */
    @GetMapping("/api/user-service/logout")
    public Result<Void> logout(@RequestParam String accessToken) {
        userLoginService.logout(accessToken);
        return Results.success();
    }
}
